/*******************************************
 * Copyright 2007 Aphrodite Framework for javaME .
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Public License v2.0
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
 * Email : owner@aphroditeme.dev.java.net
 * Contributors:
 *     Kent Elton - initial API and implementation
 *******************************************/

package com.aphrodite.ui.widgets;

import java.util.Timer;
import java.util.Vector;

import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;

/**
 * Animate an array of images
 * 
 * @author kentelt
 * 
 */
public final class ImageAnimation extends Widget {

	private final class Ani extends java.util.TimerTask {
		public void run() {
			for (; canAnimate;) {
				if (index < imageList.size() - 1)
					index++;
				else
					index = 0;
				reDrawWidget();
				try {
					Thread.sleep(delay);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}

		}
	}

	private boolean canAnimate;
	private volatile int delay = 1000;
	private final Vector imageList;
	private int index;
	private Timer timer;

	/**
	 * Image animation
	 * 
	 * @param theme
	 * 
	 */
	public ImageAnimation(Theme theme) {
		super(theme, 0, 0);
		this.imageList = new Vector();
		/* we dont want the border to be painted */
	}

	/**
	 * Add a image frame to this animation
	 * 
	 * @param frame
	 * @throws NullPointerException
	 *             if <code>frame is null</code>
	 */
	public void addFrame(Image frame) {
		if (frame == null)
			throw new NullPointerException();
		if (imageList.isEmpty()) {
			this.width = frame.getWidth();
			this.height = frame.getHeight();
			startAnimation();
		}
		imageList.addElement(frame);
		System.out.println("total frames " + imageList.size());
	}

	public void cleanUp() {
		canAnimate = false;
		imageList.removeAllElements();

	}

	public final void render(Graphics g) {
		if (!isVisible)
			return;
		capture(g, width, height, false);
		if (!imageList.isEmpty())
			g.drawImage((Image) imageList.elementAt(index), 0, 0, 0);
		release(g, width, height, false);

	}

	/**
	 * Delay between individual frames
	 * 
	 * @param delay
	 *            in millisecs
	 */
	public final void setDelay(int delay) {
		this.delay = delay;
	}

	public final void setVisible(boolean isVisible) {
		super.setVisible(isVisible);
		/* Stop the BG thread when not visible */
		if (!isVisible) {
			stopAnimation();
		} else {
			startAnimation();
		}
		reDrawWidget();
	}

	public final void startAnimation() {
		if (imageList.size() > 0)
			if (!canAnimate) {
				canAnimate = true;
				timer = new Timer();
				timer.schedule(new Ani(), 0);
			}
		// if (!canAnimate) {
		// canAnimate = true;
		// new Thread(new Runnable() {
		//
		// public void run() {
		//
		// int aniDelay = delay;/* a precaution towards thread safety */
		// for (; canAnimate;) {
		// setImageIndex();
		// reDrawWidget();
		// System.out.println("Thread count :" + Thread.activeCount());
		// // System.out.println("Animating");
		// try {
		// Thread.sleep(aniDelay);// delay);
		// } catch (InterruptedException e) {
		// e.printStackTrace();
		// }
		// }
		//System.out.println("[ImageAnimation.startAnimation] Animation stopped"
		// );
		// }
		// }).start();
		// }
	}

	public void stopAnimation() {
		// canAnimate = false;
		if (canAnimate) {
			canAnimate = false;
			timer.cancel();
			timer = null;
			stateChanged(this);
		}

	}

	protected void updateModel() {

	}

}
